<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 消除游戏 -->
    <script>
      var lastRemaining = function (n) {
        let first = 1
        let dir = 0
        let step = 1
        let cnt = n
        while (cnt > 1) {
          if (dir % 2 === 0) {
            first = first + step
          } else {
            first = cnt % 2 === 0 ? first : first + step
          }
          dir++
          // 位移操作
          cnt = cnt >> 1
          step = step << 1
        }
        return first
      }
      console.log(lastRemaining(9))
    </script>
  </body>
</html>
